package com.my.netty.nio.net;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

@Slf4j
public class NioWriteClient {

    /*
        演示服务端selector可写事件的客户端，一连接成功就接收服务端大量数据
     */
    public static void main(String[] args) throws IOException {
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost", 8080));

        ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
        //总接收字节数
        long total = 0;
        //接收服务端发送的数据
        while (true) {
            int len = sc.read(buffer);
            total += len;
            log.debug("收到服务端 {} 发送 {} 字节数据，总计 {} 字节", sc.getRemoteAddress(), len, total);
            buffer.clear();
        }
    }
}
